package view.components
{
	import events.CircleTimerEvent;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	
	import global.Resource;
	
	import utils.EnterFrameTimer;
	
	public class CircleTimer extends Sprite
	{
		private static const PIA:Number = 3.1415 / 180;
		
		private var bg:DisplayObject;
		private var bgMask:Shape;
		private var hint:DisplayObject;
		private var _leftTime:int;
		private var _totalTime:int = 15;
		private var timer:EnterFrameTimer;
		private var hintTimer:EnterFrameTimer;
		private var bgRadius:Number;
		private var increment:int = 1;
		private var setter:int = 1;
		
		private var circleEvent:CircleTimerEvent;
		
		public function CircleTimer()
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			initBg();
			initTimer();
			initHintTimer();
		}
		
		private function initHintTimer():void
		{
			hintTimer = new EnterFrameTimer(50);
			hintTimer.addEventListener(TimerEvent.TIMER,onHintTimer);
		}
		
		private function onHintTimer(event:TimerEvent):void
		{
			//hint.visible = (hint.visible)?false:true;
			hint.visible = true;
			hint.alpha = 0.2 * increment;
			
			increment += setter;	
			
			if (increment == 5)
			{
				setter = -1;
			}
			
			if (increment == 0)
			{
				setter = 1;
			}
			circleEvent = new CircleTimerEvent(CircleTimerEvent.CIRCLE_HINT);
			dispatchEvent(circleEvent);
		}
		
		private function initBg():void
		{
			
			hint = Resource.getImg("panel_circleHint");
			addChild(hint);
			hint.visible = false;
			bg = Resource.getImg("panel_circleBg");
			addChild(bg); 
			bgMask = new Shape();
			addChild(bgMask);
			bgRadius = bg.width * .5;
			bg.mask = bgMask;
			
			bg.addEventListener(Event.COMPLETE,onBgComplete);
		}
		
		private function onBgComplete(event:Event):void
		{
			event.target.removeEventListener(Event.COMPLETE,onBgComplete);
			bgRadius = bg.width * .5;
		}
		
		private function initTimer():void
		{
			timer = new EnterFrameTimer(1000);
			timer.addEventListener(TimerEvent.TIMER,onTimer);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			drawMask(_leftTime / _totalTime);
			_leftTime--;
			if (_leftTime <= 0)
			{
				stop();
				dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
			}
			else
			{
				dispatchEvent(event);
			}
			
			if (_leftTime <= 5)
			{
				hintTimer.start();
			}
		}
		
		private function drawMask(percent:Number):void
		{
			var angle:int = 360 * percent + 90;
			var i:int;
			with(bgMask.graphics)
			{
				clear();
				lineStyle(1,0);
				moveTo(bgRadius,bgRadius);
				beginFill(0xFF0000);
				for (i=90;i<angle;i++)
				{
					lineTo( bgRadius * Math.cos((360 - i) * PIA) + bgRadius, bgRadius * Math.sin((360 - i) * PIA) + bgRadius); 
				}
				lineTo(bgRadius,bgRadius);
				lineTo(bgRadius,bgRadius);
				endFill();
			}
			
		}
		
		public function start():void
		{
			timer.start();
			drawMask(_leftTime/_totalTime);
		}
		
		public function stop():void
		{
			timer.reset();
			//_leftTime = _totalTime;
			drawMask(1);
			hintTimer.stop();
			hint.visible = false;
		}
		
		public function set totalTime(v:int):void
		{
			_totalTime = v ;
		}
		
		public function get leftTime():int
		{
			return _leftTime;
		}
		
		public function set leftTime(v:int):void
		{
			_leftTime = v ;
		}
	}
}